package com.qf.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.qf.feign.GoodsFeign;
import com.qf.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@RestController
@RequestMapping("/order")
public class OrderController {


    @Autowired
    OrderService orderService;

    @RequestMapping("/testCache")
    public String testCache(){
        System.out.println(orderService.testCache(1));
        System.out.println(orderService.testCache(1));
        System.out.println(orderService.testCache(1));
        System.out.println(orderService.testCache(1));
        orderService.clearCache(1);
        System.out.println(orderService.testCache(1));
        System.out.println(orderService.testCache(1));
        return "success";
    }

//    @Autowired
//    EurekaClient eurekaClient;
//
//    @Autowired
//    RestTemplate restTemplate;

    @Autowired
    GoodsFeign goodsFeign;

    @RequestMapping("/sayHello")
    //兜底方法： 当处理器方法出现问题时，执行的方法
    @HystrixCommand(fallbackMethod = "fallback",
    commandProperties = {
            @HystrixProperty(name = "execution.isolation.strategy",value = "THREAD"),
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1000")
    } )
    public String sayHello(Integer id){

//        InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("QF-GOODS", false);
//
//        String url = instanceInfo.getHomePageUrl();
//        System.out.println(url);
//        String url = "http://QF-GOODS";
//        String url = "http://localhost:8001/goods/sayHello";
//        String action = "/goods/sayHello";

        //如何 调用 goods项目获取goods数据
        //参数1：url路径，  参数2：返回值类型
        //返回值： 响应实体对象
//        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url + action, String.class);

        //body是 响应的数据(处理器 返回值)
//        String body = responseEntity.getBody();
//        return "Order Hello!!!" + body;

//        return null;

//        String goodsBody = goodsFeign.get2(1);

//        Goods goods = new Goods();
////        goods.setId(1);
////        goods.setName("华为P40");
////        goods.setPrice(10000.0);
//////        System.out.println(1/0);
////        String goodsBody = goodsFeign.add(goods);

        String body = goodsFeign.get(id);
        return "Order Hello!!!" + body;
    }

    //服务降级的方法，必须与原方法 参数，返回值 一致
    public String fallback(Integer id) {
        System.out.println("服务降级");
        return "服务降级了......";
    }
}
